package com.example.tms.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.tms.common.annotation.LogAction;
import com.example.tms.common.enums.Result;
import com.example.tms.entity.EmployeeProject;
import com.example.tms.service.EmployeeProjectService;

@RestController
@RequestMapping("/employee-projects")
public class EmployeeProjectController {

    @Autowired
    private EmployeeProjectService employeeProjectService;

    // GET 请求：获取所有项目
    @LogAction("获取所有员工项目信息")
    @GetMapping("")
    public Result selectAll(EmployeeProject employeeProject) {
        List<EmployeeProject> list = employeeProjectService.selectAll();
        return Result.success(list);
    }

    // GET: 根据 ID 获取项目
    @LogAction("根据ID获取员工项目信息")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        EmployeeProject employeeProject = employeeProjectService.selectById(id);
        return Result.success(employeeProject);
    }

    // POST: 添加项目
    @LogAction("添加员工项目信息")
    @PostMapping("")
    public Result add(@RequestBody EmployeeProject employeeProject) {
        int result = employeeProjectService.insert(employeeProject);
        return result > 0 ? Result.success() : Result.error("添加失败");
    }

    // PUT: 更新项目信息
    @LogAction("更新员工项目信息")
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody EmployeeProject employeeProject) {
        employeeProject.setId(id); // 将路径参数的 id 设置到 employeeProject 对象中
        int result = employeeProjectService.update(employeeProject);
        return result > 0 ? Result.success() : Result.error("更新失败");
    }

    //根据员工ID获取员工项目信息
    @LogAction("根据员工ID获取其项目信息")
    @GetMapping("/employees/{empId}")
    public Result selectByEmpId(@PathVariable Integer empId) {
        EmployeeProject employeeProject = new EmployeeProject();
        employeeProject.setEmpId(empId);
        List<EmployeeProject>  list =  employeeProjectService.selectByEmpId(employeeProject);
        return Result.success(list);
    }

    //根据项目ID获取员工信息
    @LogAction("根据项目ID获取相关员工")
    @GetMapping("/projects/{proId}")
    public Result selectByProId(@PathVariable Integer proId) {
        EmployeeProject employeeProject = new EmployeeProject();
        employeeProject.setProId(proId);
        List<EmployeeProject>  list =  employeeProjectService.selectByProjectId(proId);
        return Result.success(list);
    }

    // 删除员工项目
    @LogAction(value = "删除员工项目", level = "WARN")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        int result = employeeProjectService.deleteById(id);
        return result > 0 ? Result.success() : Result.error("删除失败");
    }
}
